package com.springboot.study;

import sun.print.Win32PrintService;

import javax.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.*;
import java.awt.print.PrinterJob;
import java.io.*;

/**
 * Copyright: Copyright (c) 2019 qxintech
 *
 * @ClassName: Test
 * @Description: 该类的功能描述
 * @version: v1.0.0
 * @author: fengxx
 * @date: 2019/7/4 9:29
 * <p>
 * Modification History:
 * Date Author Version Description
 * ---------------------------------------------------------*
 * 2019/7/4 fengxx v1.0.0 新增
 */
public class Test {
    public static void main(String[] args) throws PrintException {
        JPGPrint(new File("C:\\Users\\qxkj\\Desktop\\B1.jpg"), "HP Color");
    }

    public static void JPGPrint(File file, String printerName) throws PrintException {
        if (file == null) {
            System.err.println("缺少打印文件");
        }
        InputStream fis = null;
        try {
            // 设置打印格式，如果未确定类型，可选择autosense
            DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
            // 设置打印参数
            PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
            //份数
            aset.add(new Copies(1));
            //纸张
//            aset.add(MediaSize.ISO.A4);

            aset.add(MediaTray.TOP);
            //装订
//             aset.add(Finishings.STAPLE);
            //单双面
//            aset.add(Sides.DUPLEX);

            // 定位打印服务
            PrintService printService = null;
            if (printerName != null) {
                //获得本台电脑连接的所有打印机
                PrintService[] printServices = PrinterJob.lookupPrintServices();
                if(printServices == null || printServices.length == 0) {
                    System.out.print("打印失败，未找到可用打印机，请检查。");
                    return ;
                }
                //匹配指定打印机
                for (int i = 0;i < printServices.length; i++) {
                    System.out.println(printServices[i].getName());
                    if (printServices[i].getName().contains(printerName)) {
                        printService = printServices[i];
                        break;
                    }
                }
                if(printService==null){
                    System.out.print("打印失败，未找到名称为" + printerName + "的打印机，请检查。");
                    return ;
                }
            }
            PrintServiceAttributeSet attribues = printService.getAttributes();
            MediaTray mediaTray = ((Win32PrintService) printService).findMediaTray(0);
            System.out.println(mediaTray.getName());
            // 构造待打印的文件流
            fis = new FileInputStream(file);
            Doc doc = new SimpleDoc(fis, flavor, null);
            // 创建打印作业
            DocPrintJob job = printService.createPrintJob();
//            job.print(doc, aset);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } finally {
            // 关闭打印的文件流
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
